package com.neusoft.neuiotms.dcam.fixed.converter;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.neuiotms.dcam.common.repository.LocationRepository;
import com.neusoft.neuiotms.dcam.common.repository.ManufacturerRepository;
import com.neusoft.neuiotms.dcam.fixed.dto.AssetDTO;
import com.neusoft.neuiotms.dcam.fixed.dto.InventoryResultDTO;
import com.neusoft.neuiotms.dcam.fixed.model.InventoryResult;
import com.neusoft.neuiotms.dcam.fixed.model.enums.InventoryDutyResultStatus;
import com.neusoft.neuiotms.dcam.fixed.repository.AssetTypeRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.ContractRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.InventoryDutyRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.InventoryResultRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.MaintenanceRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.ProductRepository;
import com.neusoft.neuiotms.dcam.fixed.vo.InventoryResultVO;

@Component
public class InventoryResultConverter extends AbstractConverter<InventoryResult, InventoryResultDTO> {

    @Autowired
    private InventoryResultRepository inventoryDutyResultRepository;
    @Autowired
    private InventoryDutyRepository inventoryDutyRepository;
    @Autowired
    private LocationRepository locationRepository;
    @Autowired
    private AssetTypeRepository assetTypeRepository;
    @Autowired
    private ContractRepository contractRepository;
    @Autowired
    private ManufacturerRepository manufacturerRepository;
    @Autowired
    private MaintenanceRepository maintenanceRepository;
    @Autowired
    private ProductRepository productRepository;

    @Override
    public InventoryResult toModel(final InventoryResultDTO dto) {

        InventoryResult model;
        if (dto.isNew()) {

            model = new InventoryResult();
            model.setAssetId(dto.getAssetId());
            model.setName(dto.getName());
            model.setCode(dto.getCode());
            model.setUserId(dto.getUserId());
            model.setUserName(dto.getUserName());
            model.setOrgId(dto.getOrgId());
            model.setOrgName(dto.getOrgName());
            model.setMeasureUnit(dto.getMeasureUnit());
            model.setSequence(dto.getSequence());
            model.setMoney(dto.getMoney());
            model.setMonth(dto.getMonth());
            model.setNote(dto.getNote());
            model.setSource(dto.getSource());
            model.setStatus(dto.getStatus());

            model.setExt1(dto.getExt1());
            model.setExt2(dto.getExt2());
            model.setExt3(dto.getExt3());
            model.setExt4(dto.getExt4());
            model.setExt5(dto.getExt5());

            // 位置
            if (!StringUtils.isEmpty(dto.getLocationCode())) {
                try {
                    model.setLocation(this.locationRepository.findByCode(dto.getLocationCode()));
                } catch (final Exception e) {
                    throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND,
                            new Object[] {dto.getLocationCode() });
                }
            }

            // 类型
            if (!StringUtils.isEmpty(dto.getTypeCode())) {
                try {
                    model.setType(this.assetTypeRepository.findByCode(dto.getTypeCode()));
                } catch (final Exception e) {
                    throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {dto.getTypeCode() });
                }
            }

            // 合同
            if (!StringUtils.isEmpty(dto.getContractCode())) {
                try {
                    model.setContract(this.contractRepository.findByCode(dto.getContractCode()));
                } catch (final Exception e) {
                    throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND,
                            new Object[] {dto.getContractCode() });
                }
            }

            // 供应商
            if (null != dto.getManufacturerId()) {
                try {
                    model.setManufacturer(this.manufacturerRepository.getOne(dto.getManufacturerId()));
                } catch (final Exception e) {
                    throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND,
                            new Object[] {dto.getManufacturerId() });
                }
            }

            // 维保
            if (null != dto.getMaintenanceId()) {
                try {
                    model.setMaintenance(this.maintenanceRepository.getOne(dto.getMaintenanceId()));
                } catch (final Exception e) {
                    throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND,
                            new Object[] {dto.getMaintenanceId() });
                }
            }

            // 产品
            if (null != dto.getProductId()) {
                try {
                    model.setProduct(this.productRepository.getOne(dto.getProductId()));
                } catch (final Exception e) {
                    throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {dto.getProductId() });
                }
            }

            // 任务
            if (null != dto.getDutyId()) {
                try {
                    model.setInventoryDuty(this.inventoryDutyRepository.getOne(dto.getDutyId()));
                } catch (final Exception e) {
                    throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {dto.getDutyId() });
                }
            }

        } else {

            // 查询原始数据
            model = this.inventoryDutyResultRepository.getOne(dto.getId());
            model.setAssetId(dto.getAssetId());

            // 变更地址
            if (!StringUtils.isEmpty(dto.getUpdateLocationCode())) {
                try {
                    model.setUpdateLocation(this.locationRepository.findByCode(dto.getUpdateLocationCode()));
                } catch (final Exception e) {
                    throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND,
                            new Object[] {dto.getUpdateLocationCode() });
                }
            }

            // 变更部门
            model.setUpdateOrgCode(dto.getUpdateOrgCode());
            model.setUpdateOrgName(dto.getUpdateOrgName());

            // 变更使用人
            model.setUpdateUserId(dto.getUpdateUserId());
            model.setUpdateUserName(dto.getUpdateUserName());

        }

        model.setDutyResultStatus(dto.getDutyResultStatus());

        return model;
    }

    @Override
    public InventoryResultDTO toDTO(final InventoryResult model, final boolean forListView) {
        final InventoryResultDTO dto = new InventoryResultDTO();
        dto.setId(model.getId());
        dto.setAssetId(model.getAssetId());
        dto.setName(model.getName());
        dto.setCode(model.getCode());
        dto.setUserId(model.getUserId());
        dto.setUserName(model.getUserName());
        dto.setOrgId(model.getOrgId());
        dto.setOrgName(model.getOrgName());
        dto.setMeasureUnit(model.getMeasureUnit());
        dto.setSequence(model.getSequence());
        dto.setMoney(model.getMoney());
        dto.setMonth(model.getMonth());
        dto.setNote(model.getNote());
        dto.setDutyResultStatus(model.getDutyResultStatus());
        dto.setSource(model.getSource());
        dto.setStatus(model.getStatus());

        dto.setPlanId(model.getInventoryDuty().getInventoryPlan().getId());
        dto.setPlanCode(model.getInventoryDuty().getInventoryPlan().getCode());
        dto.setPlanName(model.getInventoryDuty().getInventoryPlan().getName());
        dto.setPlanStatus(model.getInventoryDuty().getInventoryPlan().getStatus().name());

        dto.setUpdateOrgCode(model.getUpdateOrgCode());
        dto.setUpdateOrgName(model.getUpdateOrgName());
        dto.setUpdateUserId(model.getUpdateUserId());
        dto.setUpdateUserName(model.getUpdateUserName());

        // 位置
        if (model.getLocation() != null) {
            dto.setLocationCode(model.getLocation().getCode());
            dto.setLocationName(model.getLocation().getName());
        }

        // 类型
        if (null != model.getType()) {
            dto.setTypeCode(model.getType().getCode());
            dto.setTypeName(model.getType().getName());
        }

        // 合同
        if (model.getContract() != null) {
            dto.setContractCode(model.getContract().getCode());
            dto.setContractName(model.getContract().getName());
        }
        //产品
        if (model.getProduct() != null) {
            dto.setProductId(model.getProduct().getId());
            dto.setProductName(model.getProduct().getName());
        }

        // 厂商
        if (model.getManufacturer() != null) {
            dto.setManufacturerId(model.getManufacturer().getId());
            dto.setManufacturerName(model.getManufacturer().getName());
        }

        //维保
        if (model.getMaintenance() != null) {
            dto.setMaintenanceId(model.getMaintenance().getId());
            dto.setMaintenanceEnd(model.getMaintenance().getMaintenanceEnd());
            dto.setMaintenanceStart(model.getMaintenance().getMaintenanceStart());
            dto.setMaintenanceUserId(model.getMaintenance().getMaintenanceUserId());
            dto.setMaintenanceUserName(model.getMaintenance().getMaintenanceUserName());
            dto.setMaintenanceNote(model.getMaintenance().getMaintenanceNote());
        }

        // 更新的位置信息
        if (null != model.getUpdateLocation()) {
            dto.setUpdateLocationCode(model.getUpdateLocation().getCode());
            dto.setUpdateLocationName(model.getUpdateLocation().getName());
        }

        // 任务信息
        if (null != model.getInventoryDuty()) {
            dto.setDutyId(model.getInventoryDuty().getId());
            dto.setDutyName(model.getInventoryDuty().getName());
            dto.setDutyCode(model.getInventoryDuty().getCode());
            dto.setDutyUserId(model.getInventoryDuty().getUserId());
            dto.setDutyUserName(model.getInventoryDuty().getUserName());
        }
        return dto;
    }

    public InventoryResultDTO toDTO(final AssetDTO source, final Long dutyId) {

        final InventoryResultDTO dto = new InventoryResultDTO();
        dto.setAssetId(source.getId());
        dto.setName(source.getName());
        dto.setCode(source.getCode());
        dto.setLocationCode(source.getLocationCode());
        dto.setTypeCode(source.getAssetTypeCode());
        if (null != source.getContract()) {
            dto.setContractCode(source.getContract().getCode());
        }
        dto.setUserId(source.getUserId());
        dto.setUserName(source.getUserName());
        dto.setOrgId(source.getOrgId());
        dto.setOrgName(source.getOrgName());
        dto.setProductId(source.getProductId());
        dto.setManufacturerId(source.getManufacturerId());
        dto.setMeasureUnit(source.getMeasureUnit());
        dto.setSequence(source.getSequence());
        dto.setMoney(source.getMoney());
        dto.setMonth(source.getMonth());
        dto.setDutyResultStatus(InventoryDutyResultStatus.wait);
        dto.setDutyId(dutyId);
        dto.setStatus(StringUtils.isEmpty(source.getStatusCode()) ? "idle"
                : source.getStatusCode());
        dto.setSource(StringUtils.isEmpty(source.getSourceCode()) ? "purchase"
                : source.getSourceCode());
        return dto;
    }

    public InventoryResultVO toVO(final InventoryResult model) {

        final InventoryResultVO vo = new InventoryResultVO();
        vo.setName(model.getName());
        vo.setCode(model.getCode());
        vo.setAssetTypeName(model.getType().getName());
        vo.setUserName(model.getUserName());
        vo.setOrgName(model.getOrgName());
        vo.setMeasureUnit(model.getMeasureUnit());
        vo.setMoney(model.getMoney());
        vo.setMonth(model.getMonth());
        vo.setSequence(model.getSequence());
        vo.setResultStatus(model.getDutyResultStatus());

        // 供应商
        if (model.getManufacturer() != null) {
            vo.setManufacturerName(model.getManufacturer().getName());
        }

        if (null != model.getUpdateLocation()) {
            vo.setUpdateLocationName(model.getUpdateLocation().getName());
        }
        vo.setUpdateUserName(model.getUpdateUserName());
        vo.setUpdateOrgName(model.getUpdateOrgName());
        return vo;
    }

    public AssetDTO toDTO(final InventoryResultDTO source) {
        final AssetDTO dto = new AssetDTO();

        dto.setName(source.getName());
        dto.setCode(source.getCode());
        dto.setLocationCode(source.getLocationCode());
        dto.setAssetTypeCode(source.getTypeCode());
        dto.setContractCode(source.getContractCode());
        dto.setUserId(source.getUserId());
        dto.setUserName(source.getUserName());
        dto.setOrgId(source.getOrgId());
        dto.setOrgName(source.getOrgName());
        dto.setProductId(source.getProductId());
        dto.setManufacturerId(source.getManufacturerId());
        dto.setMeasureUnit(source.getMeasureUnit());
        dto.setSequence(source.getSequence());
        dto.setMoney(source.getMoney());
        dto.setMonth(source.getMonth());
        dto.setStatusCode(StringUtils.isEmpty(source.getStatus()) ? "idle"
                : source.getStatus());
        dto.setSourceCode(StringUtils.isEmpty(source.getSource()) ? "purchase"
                : source.getSource());

        return dto;

    }

    public InventoryResult toModel(final InventoryResultVO vo) {
        final InventoryResult model = new InventoryResult();
        model.setName(vo.getName());
        model.setCode(vo.getCode());
        model.setUserName(vo.getUserName());
        model.setOrgName(vo.getOrgName());
        model.setMeasureUnit(vo.getMeasureUnit());
        model.setMoney(vo.getMoney());
        model.setMonth(vo.getMonth());
        model.setSequence(vo.getSequence());
        model.setUpdateUserName(vo.getUpdateUserName());
        model.setUpdateOrgName(vo.getUpdateOrgName());
        return model;
    }
}
